package com.ruoyi.quartz.task;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.io.File;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

/**    public void mySqlDump(String host, String port, String username, String password, String databasename,String backupNumStr) throws Exception {

        String dir = RuoYiConfig.getDataBaseBackUp();
        if (!FileUtil.exist(dir)) {
            FileUtil.mkdir(dir);
        }

        String sqlName = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN)+ ".sql";
        String sqlPathName = dir + File.separator + sqlName;
        if (FileUtil.exist(sqlPathName)) {
            System.out.println(sqlName + "文件名已存在，请更换");
            return;
        }

        //拼接cmd命令  windows下 cmd   Linux下 /bin/sh
        Process exec;
        OsInfo osInfo = SystemUtil.getOsInfo();
        //如果不能取得系统属性os.name（因为Java安全限制），则总是返回false
        if(osInfo.isWindows()){
            exec = Runtime.getRuntime().exec("cmd /c mysqldump -h" + host + " -P" + port + " -u " + username + " -p" + password + " " + databasename + " > " + sqlPathName);
        } else {
            exec = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "/usr/bin/mysqldump -h" + host + " -P" + port + " -u " + username + " -p" + password + " " + databasename + " > " + sqlPathName});
        }
        if (exec.waitFor() == 0) {
            System.out.println("数据库备份成功,备份路径为：" + sqlPathName);
        }

        //压缩
        String zipPathName = dir + File.separator + sqlName + ".zip";
        ZipUtil.zip(sqlPathName, zipPathName);
        //删除
        FileUtil.del(sqlPathName);

        //获取外部的备份保留份数
        int backupNum = 5;
        try {
            backupNum = NumberUtil.parseInt(backupNumStr);
        } catch (Exception e){
            System.out.println("backupNumStr非数字:"+backupNumStr);
        }

        //删除之前备份份数
        List<String> fileNames = FileUtil.listFileNames(dir);
        if(CollUtil.isNotEmpty(fileNames)){
            //从大到小排序
            fileNames = fileNames.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
            for (int i = 0; i < fileNames.size(); i++) {
                //超过的份数全删除
                if(i>=backupNum){
                    FileUtil.del(dir + File.separator + fileNames.get(i));
                }
            }
        }

    } **/
}
